/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 欢迎到知启蒙网站（https://www.zhiqim.com）购买正版软件，知启蒙还提供许多开源框架和软件。
 * 
 * 1、本软件产品所有源代码受《中华人民共和国著作权法》和其他有关法律、法规的保护，其所有知识产权归湖南知启蒙科技有限公司所有；
 * 2、禁止复制和修改。不得复制修改、翻译或改编本软件所有源代码，或者基于本软件产品创作衍生作品；
 * 3、禁止进行逆向工程。不得对本软件的源代码进行逆向工程、反编译或试图以其他方式发现软件的源代码；
 * 4、个别授权：如需进行商业性的销售、复制、分发，包括但不限于软件销售、预装、捆绑等，必须获得知启蒙的书面授权和许可；
 * 5、保留权利：本注释未明示授权的其他一切权利仍归知启蒙所有，任何人使用其他权利时必须获得知启蒙的书面同意。
*/
package net.redxyz.repost.manage.action.app;

import java.util.List;

import net.redxyz.top.dbo.sys.SysTmcCat;
import net.redxyz.top.dbo.sys.SysTmcTopic;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.httpd.validate.ones.IsSelect;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * tmc消息主题管理
 *
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
public class TmcTopicAction extends StdSwitchAction
{
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("data", "请选择一个选项"));
    }
    
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsSelect("catId", "请选择主题类型"));
        request.addValidate(new IsNotEmpty("topicId", "主题编号不能为空"));
        request.addValidate(new IsNotEmpty("topicName", "主题名称不能为空"));
        request.addValidate(new IsSelect("topicStatus", "请选择主题状态"));
        request.addValidate(new IsSelect("isUserPermit", "请选择主是否需要授权"));
    }

    protected void list(HttpRequest request) throws Exception
    {
        List<SysTmcCat> tmcCatList = ORM.table().list(SysTmcCat.class);
        request.setAttribute("tmcCatList", tmcCatList);
        
        if (tmcCatList.isEmpty())
        {
            request.setAttribute("catId", "");
            request.setAttribute("topicList", null);
            return;
        }
        
        String catId = request.getParameter("catId", tmcCatList.get(0).getCatId());
        request.setAttribute("catId", catId);
        
        Selector selector = new Selector();
        selector.addMust("catId", catId);
        selector.addOrderbyAsc("topicStatus");
        List<SysTmcTopic> topicList = ORM.table().list(SysTmcTopic.class, selector);
        request.setAttribute("topicList", topicList);
    }

    protected void add(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        List<SysTmcCat> tmcCatList = ORM.table().list(SysTmcCat.class);
        request.setAttribute("tmcCatList", tmcCatList);
    }

    protected void modify(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        
        String data = request.getParameter("data");
        if (Validates.isEmpty(data))
        {
            request.returnHistory("请选择一条主题");
            return;
        }
        
        String[] ret = data.split(",");
        
        SysTmcTopic topic = ORM.table().item(SysTmcTopic.class, ret[0], ret[1]);
        request.setAttribute("topic", topic);
        List<SysTmcCat> tmcCatList = ORM.table().list(SysTmcCat.class);
        request.setAttribute("tmcCatList", tmcCatList);
    }

    protected void insert(HttpRequest request) throws Exception
    {
        SysTmcTopic topic = request.getParameter(SysTmcTopic.class);
        
        ORM.table().insert(topic);
    }

    protected void update(HttpRequest request) throws Exception
    {
        SysTmcTopic topic = request.getParameter(SysTmcTopic.class);
        
        Updater updater = new Updater();
        updater.addMust("topicId", topic.getTopicId());
        updater.addMust("catId", topic.getCatId());
        updater.addField("topicName", topic.getTopicName());
        updater.addField("topicStatus", topic.getTopicStatus());
        updater.addField("isUserPermit", topic.getIsUserPermit());
        
        ORM.table().update(SysTmcTopic.class, updater);
    }

    protected void delete(HttpRequest request) throws Exception
    {
        String data = request.getParameter("data");
        if(Validates.isNotEmpty(data))
        {
            String[] ret = data.split(",");
            ORM.table().delete(SysTmcTopic.class, ret[0], ret[1]);
        }
    }
}
